package com.singzi.leetcode.N900;

public class T0875 {
    public static void main(String[] args) {
        Solution0875 solution0875 = new Solution0875();
        solution0875.minEatingSpeed(new int[]{3,6,7,11}, 8);
    }
}
class Solution0875 {
    public int minEatingSpeed(int[] piles, int h) {
        int left = 1, right = 0;
        for(int pile:piles){
            // if(pile > left) left = pile;
            right += pile;
        }
        while(left <= right){
            int mid = left + (right - left)/2;
            if(check(mid, piles, h)){
                right = mid - 1;
            }else{
                left = mid + 1;
            }
        }
        return left;
    }
    public boolean check(int mid, int []piles, int h){
        int cont = 0;
        for(int i = 0;i < piles.length;i++){
            while(piles[i] > 0){
                piles[i] -= mid;
                cont++;
            }
        }
        return cont <= h;
    }

}